package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.pojo.Customer;
import com.qf.service.CustomerService;
import com.qf.utils.AjaxMessage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：冯国清
 * 项目名：com.qf.controller
 * 时间：2020/7/27 22:04
 * 描述:
 */
@Controller
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @RequestMapping("/customer")
    @ResponseBody
    public Map<String,Object> getAllCustomer(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int limit){
        Map<String, Object> map  = null;
        List<Object> list = new ArrayList<>();
        List<Customer> allClient = customerService.getAllCustomer(page, limit);
        for (Customer c : allClient) {
            map = new HashMap<>();
            map.put("cid", c.getCid());
            map.put("cusername", c.getCusername());
            map.put("cpassword", c.getCpassword());
            map.put("cnickname", c.getCnickname());
            map.put("caddress", c.getCaddress());
            map.put("cmoney", c.getCmoney());
            if (1 == c.getCstate()) {
                map.put("cstate", "正常");
            } else {
                map.put("cstate", "停用");
            }
            list.add(map);
        }
        Map<String, Object> clientMap = new HashMap<>();
        clientMap.put("code", 0);
        clientMap.put("msg", "");
        clientMap.put("count", customerService.getCount());
        clientMap.put("data", list);
        return clientMap;
    }
@ResponseBody
    @RequestMapping("/addcustomer")
    public String saveCustomer(Customer customer){
        int i = customerService.saveCustomer(customer);
        if (i>=1){
            return "success";
        }else {
            return "failed";
        }
    }
    @RequestMapping("/selectCustomerById1/{cid}")
    public String selectCustomerById1(@PathVariable int cid,Model model){
        Customer customer = customerService.selectCustomerById(cid);
        model.addAttribute("customer",customer);
        return "/ShowCustomer.jsp";
    }
    @RequestMapping("/selectCustomerById2/{cid}")
    public String selectCustomerById2(@PathVariable int cid,Model model){
        Customer customer = customerService.selectCustomerById(cid);
        model.addAttribute("customer",customer);
        return "/UpdateCustomer.jsp";
    }
    @RequestMapping("/selectCustomerById/{cid}")
    public Customer selectCustomerById(@PathVariable int cid,Model model){
        Customer customer = customerService.selectCustomerById(cid);
        return customer;
    }
    @ResponseBody
    @RequestMapping("/updateCustomer")
    public String updateCustomer(Customer customer){
        int i = customerService.updateCustomer(customer);
        if (i>=1){
            return "success";
        }else {
            return "failed";
        }
    }
    @RequestMapping("/deleteCustomer/{cid}")
    @ResponseBody
    public String deleteCustomer(@PathVariable int cid){
        System.out.println(cid);
        int i = customerService.deleteCustomer(cid);
        if (i>=1){
            return "success";
        }
        return "failed";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxMessage delete(@RequestParam("cids")int[] cids){

        if(cids!=null&&cids.length>0){
            customerService.deleteByIds(cids);
            return new AjaxMessage(true,"删除成功");
        }
        return new AjaxMessage(false,"删除失败");
    }
}
